﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using stefab.Musique;
using System.Xml.Linq;

namespace stefab
{
    public static class LastFmApi
    {
        private static string API_KEY = "4db79051c1d8791d835a1b4ba4b80542";
        private static string urlRechercheConcertsArtiste = "http://ws.audioscrobbler.com/2.0/?method=artist.getevents&&artist=%a&&api_key=" + API_KEY;
        private static string urlRechercheConcertsSalle = "http://ws.audioscrobbler.com/2.0/?method=geo.getevents&&location=%l&&api_key=" + API_KEY;

        public static List<Event> getArtistEvents(string artistName)
        {
            List<Event> lesEvents = new List<Event>();
            string url = urlRechercheConcertsArtiste.Replace("%a", artistName);
            XDocument docXml = XDocument.Load(url);
            XElement xLfm = docXml.Element("lfm");
            XElement xEvents = xLfm.Element("events");
            IEnumerable<XElement> xLesEvents = xEvents.Elements();

            foreach (XElement unElement in xLesEvents)
            {
                XElement xVenue = unElement.Element("venue");
                XElement xLocation = xVenue.Element("location");
                String cityLocation = xLocation.Element("city").Value;
                String countryLocation = xLocation.Element("country").Value;
                String streetLocation = xLocation.Element("street").Value;
                String postalCodeLocation = xLocation.Element("postalcode").Value;
                XNamespace xGeoNamespace = "http://www.w3.org/2003/01/geo/wgs84_pos#";
                XElement xGeo = xLocation.Element(xGeoNamespace + "point");
                String geoLat = xGeo.Element(xGeoNamespace + "lat").Value;
                String geoLong = xGeo.Element(xGeoNamespace + "long").Value;
                Location uneLocation = new Location(cityLocation, countryLocation, streetLocation, postalCodeLocation, geoLat, geoLong);

                String idVenue = xVenue.Element("id").Value;
                String nameVenue = xVenue.Element("name").Value;
                String imageVenue = getUrlImage(xVenue);
                String siteVenue = xVenue.Element("website").Value;
                String telVenue = xVenue.Element("phonenumber").Value;
                Venue uneVenue = new Venue(idVenue, nameVenue, imageVenue, siteVenue, telVenue, uneLocation);

                List<Artist> lesArtistes = new List<Artist>();
                XElement xArtists = unElement.Element("artists");
                IEnumerable<XElement> xLesArtists = xArtists.Elements("artist");

                foreach (XElement xArtiste in xLesArtists)
                {
                    Artist unArtiste = new Artist(xArtiste.Value);
                    lesArtistes.Add(unArtiste);
                }

                String idEvent = unElement.Element("id").Value; //Récupère la valeur de l'id
                String titleEvent = unElement.Element("title").Value;
                String descriptionEvent = unElement.Element("description").Value;
                String imageEvent = getUrlImage(unElement);
                String dateEvent = unElement.Element("startDate").Value;
                String siteEvent = unElement.Element("url").Value;
                Event unEvent = new Event(idEvent, titleEvent, descriptionEvent, imageEvent, siteEvent, dateEvent, uneVenue, lesArtistes);
                lesEvents.Add(unEvent);
            }

            return lesEvents;
        }

        public static List<Event> getGeoEvents(string locationName)
        {
            List<Event> lesEvents = new List<Event>();
            string url = urlRechercheConcertsSalle.Replace("%l", locationName);
            XDocument docXml = XDocument.Load(url);
            XElement xLfm = docXml.Element("lfm");
            XElement xEvents = xLfm.Element("events");
            IEnumerable<XElement> xLesEvents = xEvents.Elements();

            foreach (XElement unElement in xLesEvents)
            {
                XElement xVenue = unElement.Element("venue");
                XElement xLocation = xVenue.Element("location");
                String cityLocation = xLocation.Element("city").Value;
                String countryLocation = xLocation.Element("country").Value;
                String streetLocation = xLocation.Element("street").Value;
                String postalCodeLocation = xLocation.Element("postalcode").Value;
                XNamespace xGeoNamespace = "http://www.w3.org/2003/01/geo/wgs84_pos#";
                XElement xGeo = xLocation.Element(xGeoNamespace + "point");
                String geoLat = xGeo.Element(xGeoNamespace + "lat").Value;
                String geoLong = xGeo.Element(xGeoNamespace + "long").Value;
                Location uneLocation = new Location(cityLocation, countryLocation, streetLocation, postalCodeLocation, geoLat, geoLong);

                String idVenue = xVenue.Element("id").Value;
                String nameVenue = xVenue.Element("name").Value;
                String imageVenue = getUrlImage(xVenue);
                String siteVenue = xVenue.Element("website").Value;
                String telVenue = xVenue.Element("phonenumber").Value;
                Venue uneVenue = new Venue(idVenue, nameVenue, imageVenue, siteVenue, telVenue, uneLocation);

                List<Artist> lesArtistes = new List<Artist>();
                XElement xArtists = unElement.Element("artists");
                IEnumerable<XElement> xLesArtists = xArtists.Elements("artist");

                foreach (XElement xArtiste in xLesArtists)
                {
                    Artist unArtiste = new Artist(xArtiste.Value);
                    lesArtistes.Add(unArtiste);
                }

                String idEvent = unElement.Element("id").Value; //Récupère la valeur de l'id
                String titleEvent = unElement.Element("title").Value;
                String descriptionEvent = unElement.Element("description").Value;
                String imageEvent = getUrlImage(unElement);
                String dateEvent = unElement.Element("startDate").Value;
                String siteEvent = unElement.Element("url").Value;
                Event unEvent = new Event(idEvent, titleEvent, descriptionEvent, imageEvent, siteEvent, dateEvent, uneVenue, lesArtistes);
                lesEvents.Add(unEvent);
            }

            return lesEvents;
        }

        private static string getUrlImage(XElement unElement)
        {
            IEnumerable<XElement> lesImages = from el in unElement.Elements("image") where (string)el.Attribute("size") == "large" select el;

            foreach (XElement image in lesImages)
            {
                return image.Value;
            }

            return ""; //s'il n'y a pas d'image
        }
    }
}
